package com.doudizu.shoesonline.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.doudizu.shoesonline.common.BaseResponse;
import com.doudizu.shoesonline.common.ResultUtils;
import com.doudizu.shoesonline.model.dto.type.TypeQuery;
import com.doudizu.shoesonline.model.entity.Type;
import com.doudizu.shoesonline.service.TypeService;
import com.doudizu.shoesonline.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ln
 */
@RestController
@RequestMapping("/type")
@Api(tags = "类型模块")
public class TypeController {

    @Resource
    private TypeService typeService;

    @Resource
    private UserService userService;

    @GetMapping("/list")
    @ApiOperation("查询所有的类型")
    public BaseResponse list() {
        List<Type> list = typeService.list();
        return ResultUtils.success(list);
    }

    @PostMapping("/add")
    @ApiOperation("添加类型信息")
    public BaseResponse add(@RequestBody Type type) {
        typeService.save(type);
        return ResultUtils.success(null);
    }


    /**
     *  修改状态或者修改类型信息
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "单个修改")
    public BaseResponse status(@RequestBody Type type) {
        typeService.updateById(type);
        return ResultUtils.success(null);
    }


    @GetMapping("/search")
    @ApiOperation("模糊查询类型")
    public BaseResponse getTypeMessage(String text) {
        if (StringUtils.isBlank(text)) {
            return this.list();
        }
        LambdaQueryWrapper<Type> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Type::getTypeName,text);
        List<Type> list = typeService.list(lambdaQueryWrapper);
        return ResultUtils.success(list);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询")
    public BaseResponse page(TypeQuery typeQuery) {
        return ResultUtils.success(typeService.getTypes(typeQuery));
    }

    @PostMapping("/delete")
    @ApiOperation("单个或批量删除类型信息")
    public BaseResponse delete(@RequestParam List<Integer> ids) {
        typeService.removeBatchByIds(ids);
        return ResultUtils.success(null);
    }
}
